package com.arvato.wxwork.service.member;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.wxwork.bean.bean_do.member.WxworkBindHistoryDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkMemberBindDo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkExternalInfoVo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkMemberBindVo;
import com.arvato.wxwork.bean.bean_vo.staff.WxworkStaffInfoVo;
import com.arvato.wxwork.mapper.member.WxworkBindHistoryMapper;
import com.arvato.wxwork.mapper.member.WxworkMemberBindMapper;
import com.arvato.wxwork.service.staff.WxworkStaffInfoService;


@Service
public class WxworkMemberBindService {
	
	private Logger logger = LoggerFactory.getLogger(WxworkMemberBindService.class);

	@Autowired
	@Lazy
	WxworkExternalService wxworkExternalService;
	
	@Autowired
	private WxworkMemberBindMapper wxworkMemberBindMapper;
	
	@Autowired
	private WxworkBindHistoryMapper wxworkBindHistoryMapper;
	
	@Autowired
	private WxworkStaffInfoService wxworkStaffInfoService;
	/**
	 * 批量查询员工和外部人绑定关系(不分页)
	 * @param query
	 * @return
	 */
	public List<WxworkMemberBindVo> listBindByQuery(Query query) {
		return wxworkMemberBindMapper.listBindByQuery(query);
	}
	
	/**
	 * 新增绑定关系
	 * @param memberBindDo
	 */
	public int insert(WxworkMemberBindDo memberBindDo) {
		int row = wxworkMemberBindMapper.insert(memberBindDo);
		if(row >= 1) {
			// 插入成功就改备注
			wxworkExternalService.setRemark(memberBindDo.getExternalId());
			// 记录绑定历史
			bindHistoryInsert(memberBindDo.getBindId());
		}
		return row;
	}
	
	/**
	 * 批量新增绑定关系
	 * @param memberBindDo
	 */
	public List<WxworkMemberBindDo> insertBatch(List<WxworkMemberBindDo> memberBindDoList) {
		List<WxworkMemberBindDo> failList = new ArrayList<WxworkMemberBindDo>();
		for(WxworkMemberBindDo bindDo : memberBindDoList) {
			try {
				int row = insert(bindDo);
				if(row == 0) {
					failList.add(bindDo);
				}
			} catch (Exception e) {
				e.printStackTrace();
				failList.add(bindDo);
			}
		}
		return failList;
	}
	
	/**
	 * 更新绑定关系
	 * @param memberBindDo
	 * @return
	 */
	public int update(WxworkMemberBindDo memberBindDo) {
		return wxworkMemberBindMapper.update(memberBindDo);
	}
	
	/**
	 * 更新BC给员工分配的分组
	 * @param memberBindDo
	 * @return
	 */
	public int updateBindGroup(WxworkMemberBindDo memberBindDo) {
		return wxworkMemberBindMapper.updateBindGroup(memberBindDo);
	}
	
	/**
	 * 删除一个绑定关系
	 * @param memberBindDo
	 * @return
	 */
	public int delete(String bindId) {
		return wxworkMemberBindMapper.delete(bindId);
	}
	
	/**
	 * 根据条件删除多个绑定关系
	 * @param query
	 * @return
	 */
	public int deleteByQuery(Query query) {
		return wxworkMemberBindMapper.deleteByQuery(query);
	}
	
	/**
	 * 检查当前客户是否被该品牌下的人招募走了
	 * @param externalId
	 * @param brand
	 * @return
	 */
	public boolean checkBind(String externalId, String brand) {
		Query query = new Query();
		query.put("externalId", externalId);
		List<WxworkMemberBindVo> bindList = wxworkMemberBindMapper.listBindByQuery(query);
		if(bindList.size() == 0) {
			return false;
		}
		return true;
	}
	
	/**
	 * 插入绑定解绑历史记录
	 * @param wxworkBindHistoryDo
	 * @return
	 */
	public int bindHistoryInsert(String bindId) {
		int row = 0;
		try {
			WxworkMemberBindVo bindVo = wxworkMemberBindMapper.getBindById(bindId);
			WxworkStaffInfoVo staffVo = wxworkStaffInfoService.getStaffById(bindVo.getStaffId());
			Query query = new Query();
			query.put("externalId", bindVo.getExternalId());
			List<WxworkExternalInfoVo> externalList = wxworkExternalService.listExternalByQuery(query);
			WxworkExternalInfoVo externalVo = externalList.get(0);
			WxworkBindHistoryDo wxworkBindHistoryDo = new WxworkBindHistoryDo();
			BeanUtils.copyProperties(staffVo, wxworkBindHistoryDo);
			BeanUtils.copyProperties(externalVo, wxworkBindHistoryDo);
			wxworkBindHistoryDo.setStaffName(staffVo.getName());
			wxworkBindHistoryDo.setExternalName(externalVo.getName());
			wxworkBindHistoryDo.setExternalName(externalVo.getName());
			wxworkBindHistoryDo.setBindTime(new Date());
			wxworkBindHistoryDo.setStaffStatus(staffVo.getStatus());
			wxworkBindHistoryDo.setExternalStatus(1);
			row = wxworkBindHistoryMapper.insert(wxworkBindHistoryDo);
		} catch (Exception e) {
			logger.info("记录历史发生错误", e);
			e.printStackTrace();
		}
		return row;
	}
	
	/**
	 * 更新绑定解绑的历史记录
	 * @param bindVo 
	 * @return
	 */
	public int bindHistoryUpdate(WxworkMemberBindVo bindVo) {
		int row = 0;
		try {
			if(bindVo == null) {
				return row;
			}
			WxworkStaffInfoVo staffVo = wxworkStaffInfoService.getStaffById(bindVo.getStaffId());
			Query query = new Query();
			query.put("externald", bindVo.getExternalId());
			List<WxworkExternalInfoVo> externalList = wxworkExternalService.listExternalByQuery(query);
			WxworkExternalInfoVo externalVo = externalList.get(0);
			WxworkBindHistoryDo wxworkBindHistoryDo = new WxworkBindHistoryDo();
			BeanUtils.copyProperties(externalVo, wxworkBindHistoryDo);
			wxworkBindHistoryDo.setExternalName(externalVo.getName());
			wxworkBindHistoryDo.setExternalName(externalVo.getName());
			wxworkBindHistoryDo.setUnbindTime(new Date());
			wxworkBindHistoryDo.setExternalStatus(2);
			if(staffVo != null) {
				BeanUtils.copyProperties(staffVo, wxworkBindHistoryDo);
				wxworkBindHistoryDo.setStaffName(staffVo.getName());
				wxworkBindHistoryDo.setStaffStatus(staffVo.getStatus());
			}else {
				wxworkBindHistoryDo.setStaffId(bindVo.getStaffId());
				wxworkBindHistoryDo.setStaffStatus(2);
			}
			row = wxworkBindHistoryMapper.update(wxworkBindHistoryDo);
		} catch (Exception e) {
			logger.info("记录历史发生错误", e);
			e.printStackTrace();
			return row;
		}
		return row;
	}

}
